<?php

Yii::import( "ext.xupload.models.XUploadForm" );
Yii::import( "ext.xupload.models.ImageUploadForm" );
Yii::import( "application.controllers.FrontendBaseController" );

class ExpositionController extends FrontendBaseController
{
    public function actionIndex()
    {                   
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Выставки" => "" );
        
        $expositions = ExpositionFacade::getActualExpositions();        
        $count = ExpositionFacade::getCountActualExpositions();        
        
        $this->render('exposition', array(
            "expositions"   => $expositions,
            "count"         => $count
        ));
    }
    
    public function actionEdit( $id )
    {
        $exposition = $this->loadModel( $id );
        $user = $this->getUserFromSession();
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Выставки" => "/exposition",
                                    $exposition->name => "/exposition/view/" . $id,
                                    "Редактирование"    => '');
        
        $isOwner = false;
        if ($user->user_id == $exposition->user_id)
        {
            $isOwner = true;
        }

        if (!isset($_POST['Exposition']))
        {                    
            $this->render('edit', array('model' => $exposition));
        }
        else
        {            
            $exposition->attributes = $_POST['Exposition'];                    
            $exposition->from_date = strtotime($_POST['Exposition']['from_date']);
            $exposition->to_date = strtotime($_POST['Exposition']['to_date']);
            $exposition->save();   
            
            $this->redirect('/exposition/view/' . $exposition->exposition_id);
        }                
    }
    
    public function actionView( $id )
    {
        $exposition = $this->loadModel( $id );
        $user = $this->getUserFromSession();
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Выставки" => "/exposition",
                                    $exposition->name => "");
        
        $isOwner = false;
        if ($user->user_id == $exposition->user_id)
        {
            $isOwner = true;
        }
        
        $expositionMembers = ExpositionFacade::getExpositionMembers($id);
        $expositionMembersCount = ExpositionFacade::getExpositionMembersCount($id);
        $author = UserFacade::getUserById($exposition->user_id);

        $this->render('view', array(
            'model'                     => $exposition,
            'user'                      => $user,
            'isOwner'                   => $isOwner,
            'expositionMembers'         => $expositionMembers,
            'expositionMembersCount'    => $expositionMembersCount,
            'author'                    => $author
        ));
    }
    
    public function actionGo( $id )
    {
        $exposition = $this->loadModel($id);
        
        if (!ExpositionFacade::isAlreadyGo($this->getUserIdFromSession(), $id) && !Yii::app()->user->isGuest)
        {
            ExpositionFacade::addExpositionMember($this->getUserIdFromSession(), $id);
        }
        
        $this->redirect("/exposition/view/" . $id);
    }
    
    public function actionAdd()
    {
        $user = $this->getUserFromSession();
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Выставки"          => "/exposition",
                                    "Создание выставки" => "" );
        
        if (!isset($_POST['Exposition']))
        {
            $model = new Exposition();
        
            $this->render('add', array('model' => $model));
        }
        else
        {
            $exposition = new Exposition();
            $exposition->attributes = $_POST['Exposition'];
            $exposition->user_id = $user->user_id;            
            $exposition->from_date = strtotime($_POST['Exposition']['from_date']);
            $exposition->to_date = strtotime($_POST['Exposition']['to_date']);
            $exposition->save();   
            
            if ($exposition->hasErrors())
            {
                $this->render('add', array('model' => $exposition));
            }
            
            $this->redirect('/exposition/view/' . $exposition->exposition_id);
        }
    }
    
    public function actionAddphoto( $id )
    {
        $exposition = $this->loadModel( $id );
        $user = $this->getUserFromSession();
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Выставки" => "/exposition",
                                    $exposition->name => "/exposition/view/" . $id,
                                    "Загрузка изображения" => "");
                
        if ($user->user_id != $exposition->user_id)
        {
            throw new CHttpException( 404, 'Страница не найдена' );
        }
        
        $this->render('addphoto', array(
            'exposition' => $exposition
        ));
    }
    
    public function loadModel( $id )
    {
        $model = ExpositionFacade::getModel( $id );
        if ( $model === null )
            throw new CHttpException( 404, 'Страница не найдена' );
        return $model;
    }
    
}
?>
